package practice.MyProject.service;

import practice.MyProject.bean.Custom;
import lombok.Data;

/**
 * @Author juzipi
 * @Date 2021/6/25 10:13
 * @Info
 */
@Data
public class CustomList {

    private final Custom[] customs;

    private Integer total;

    public CustomList(int total){
        this.total = 0;
        customs = new Custom[total];
    }

    public boolean addCustom(Custom custom){
        if (total >= customs.length){
            return false;
        }
        customs[total++] = custom;
        return true;
    }

    public boolean modifyCustom(int id,Custom custom){
        for (int i = 0; i < total; i++) {
            if (customs[i].getId() == id) {
                customs[i] = custom;
                System.out.println("修改成功!");
                return true;
            }
        }
        System.out.println("修改失败");
        return false;
    }

    //根据id获取客户信息
    public Custom findCustom(int id){
        if (total != null){
            for (int i = 0; i < total; i++) {
                if (customs[i].getId() == id) {
                    return customs[i];
                }
            }
        }
        System.out.println("未找到id为 "+id+" 的用户");
        return null;
    }

    public Custom[] findAllCustom(){
        Custom[] customs = new Custom[total];
        for (int i = 0; i < total; i++) {
            customs[i] = this.customs[i];
        }
        return customs;
    }

    public boolean deleteCustom(int id){
        if (total != null){
            for (int i = 0; i < customs.length; i++) {
                if (customs[i].getId() == id) {
                    customs[i] = customs[i+1];
                    customs[--total] = null;
                    return true;
                }
            }
        }
        System.out.println("删除失败");
        return false;
    }

    public int getTotalNum() {
        return total;
    }
}
